package com.carbon.assets.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.assets.entity.CarbonExchange;
import com.carbon.assets.param.ExchangeAccountBindingParam;
import com.carbon.assets.service.CarbonExchangeService;
import com.carbon.assets.service.ExchangeAccountService;
import com.carbon.assets.param.ExchangeAccountQueryParam;
import com.carbon.assets.util.CommonUtil;
import com.carbon.assets.vo.ExchangeAccountQueryReturnVo;
import com.carbon.assets.vo.ExchangeAccountQueryVo;
import com.carbon.assets.entity.ExchangeAccount;
import com.carbon.assets.common.BaseController;
import com.carbon.common.api.Paging;
import com.carbon.common.feishu.FeiShuAPI;
import com.carbon.domain.common.ApiResult;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 交易账户  前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2021-08-16
 */
@Slf4j
@RestController
@RequestMapping("/exchangeAccount")
@Api(value = "交易账户 模块", tags = {"交易账户 模块"})
public class ExchangeAccountController extends BaseController {
    /**
     * 接收前端上传的账户凭证图片并返回飞书保存路径
     */
    // 飞书文件夹token（提前在飞书创建文件夹，获取其token）
//    private static final String FEISHU_FOLDER_TOKEN = "Rri7fCuQwlMvTudAXcWcJ1Qinff";
    @Autowired
    private ExchangeAccountService exchangeAccountService;
    /**
     * 上传账户凭证图片
     */
    @PostMapping("/uploadCredential")
    public ApiResult uploadCredential(@RequestParam("file") MultipartFile file) {
        try {
            // 调用工具类上传图片到飞书，获取路径
            Map imageUrl =(Map) (CommonUtil.uploadFile(file, file.getOriginalFilename()).getData());
            String file_token = "https://tcnfo9qabjqy.feishu.cn/file/"+imageUrl.get("file_token");

            return ApiResult.ok(file_token);
        } catch (Exception e) {
            return ApiResult.fail("上传失败：");
        }

    }
    @PutMapping("/unbind/{id}")
    public ApiResult unbind(@PathVariable("id") Integer id) {
        System.out.println("1111");
        boolean b = exchangeAccountService.removeById(id);
        return ApiResult.result(b,"解绑"+(b?"成功":"失败"));
    }
    @PostMapping("/add")
    public ApiResult add(@RequestBody ExchangeAccount exchangeAccount) {
        exchangeAccount.setAccountStatus("0430000001");
        Integer row = exchangeAccountService.add(exchangeAccount);
        return ApiResult.result(row>0);
    }
    @PostMapping("/search")
    public ApiResult search(@RequestBody ExchangeAccountQueryParam param) {
        Page<ExchangeAccountQueryVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<ExchangeAccountQueryVo> paging = exchangeAccountService.search(page,param);
        ExchangeAccountQueryReturnVo exchangeAccountQueryReturnVo = new ExchangeAccountQueryReturnVo();
        exchangeAccountQueryReturnVo.setExchangeAccountQueryVoList(paging.getRecords());
        exchangeAccountQueryReturnVo.setTotal((int) paging.getTotal());
        return ApiResult.ok(exchangeAccountQueryReturnVo);
    }
}

